//FractionPanel
//Timothy Grant
//CS1410
//Calculator 1410
//Final Version
package calculator1410;

import javax.swing.JFrame;
import javax.swing.JTextArea;
import numbers.FractionNumber;
import evaluateexpression.ExpressionEvaluator;

public class FractionPanel extends NumberPanel {

    @Override
    public void toggleInfo() {
        String helpMessage = "Enter fractions using ' : ' to indicate the ratio:\n"
                + "     Enter one-half as 1:2\n\n"
                + "The following operations are supported: \n\n"
                + "     Addition: 1:2+3:4 = 10:8\n"
                + "     Subtraction: 1:2-3:4 = -2:8\n"
                + "     Multiplication: 1:2*3:4 = 3:8\n"
                + "     Division: 1:2/3:4 = 6:4\n\n"
                + "Numerator and denominator must be whole numbers\n\n"
                + "Parentheses are supported: (1:2+2:3)-(3:4*4:5)= 68:120\n\n"
                + "Fraction reduction not yet added";


        String helpHeader = "Fraction Number Help";

        JFrame helpFrame = new JFrame(helpHeader);
        JTextArea helpExp = new JTextArea(helpMessage);
        //helpExp.setFont( helpFont );
        helpFrame.add(helpExp);
        helpFrame.setResizable(false);
        helpFrame.pack();
        //helpFrame.setSize(320, 270);
        helpFrame.setVisible(true);
    }

    @Override
    public void equals() {
        FractionNumber fraction = new FractionNumber(0, 1);
        String equal;
        if (!expressionArea.getText().contains("=")) {
            equal = ExpressionEvaluator.evalExpression(
                    expressionArea.getText(), fraction);
            expressionArea.setText(expressionArea.getText() + "\n=\n" + equal);
        } else {
            int equalIndex = expressionArea.getText().indexOf("=");
            equal = ExpressionEvaluator.evalExpression(
                    expressionArea.getText().substring(++equalIndex), fraction);
            expressionArea.setText(expressionArea.getText().substring(++equalIndex)
                    + "\n=\n" + equal);
        }
    }

    @Override
    public void newEquals() {
        FractionNumber fraction = new FractionNumber(0, 1);
        String equal;
        if (!expressionArea.getText().contains("=")) {
            equal = ExpressionEvaluator.evalExpression(
                    expressionArea.getText(), fraction);
            expressionArea.setText(expressionArea.getText() + "\n=\n" + equal);
        } else {
            int equalIndex = expressionArea.getText().indexOf("=");
            equal = ExpressionEvaluator.evalExpression(
                    expressionArea.getText().substring(++equalIndex), fraction);
            expressionArea.setText(expressionArea.getText().substring(++equalIndex)
                    + "\n=\n" + equal);
        }
    }
}
